/**
 * 
 */
package com.platform.template.mongo;

import java.text.MessageFormat;

import javax.annotation.PostConstruct;

import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.platform.common.utils.Assert;

/**
 * <pre>
 * 功能：MongoDb的工具类
 * 
 * &lt;bean id="mongoDBTemplate" class="com.platform.template.mongo.MyMongoTemplate"&gt;
 * 		&lt;property name="username" value="${mongo.username}" /&gt;
 * 		&lt;property name="password" value="${mongo.password}" /&gt;
 * 		&lt;property name="address" value="${mongo.address}" /&gt;
 * 		&lt;property name="authdbname" value="${mongo.authdbname}" /&gt;
 * 		&lt;property name="dbname" value="${mongo.dbname}" /&gt;
 * &lt;/bean&gt;
 * 
 * 在properties文件中配置：
 * mongo.username
 * mongo.password
 * mongo.address
 * mongo.authdbname
 * mongo.dbname
 * 
 * </pre>
 * @author wei.gao
 */
public class MyMongoTemplate {
	// 用户名
	private String username;
	// 密码
	private String password;
	// 连接地址
	private String address;
	// 认证数据库名
	private String authdbname;
	// 数据库名
	private String dbname;
	
	@PostConstruct
	private void init(){
		Assert.notEmpty("username", username);
		Assert.notEmpty("password", password);
		Assert.notEmpty("address", address);
		Assert.notEmpty("authdbname", authdbname);
		Assert.notEmpty("dbname", dbname);
	}
	
	// 
	private MongoTemplate template;
	
	/**
	 * <pre>
	 * 功能：获取MongoTemplate对象
	 * </pre>
	 * @return
	 */
	public MongoTemplate getTemplate(){
		if(template == null){
			synchronized (MyMongoTemplate.class) {
				if(template == null){
					template = new MongoTemplate(getMongoDbFactory());
				}
			}
		}
		return template;
	}
	
	// 
	private MongoClient mongoClient;
	
	/**
	 * <pre>
	 * 功能：获取MongoClient对象
	 * </pre>
	 * @return
	 */
	public MongoClient getMongoClient(){
		if(mongoClient == null){
			synchronized (MyMongoTemplate.class) {
				if(mongoClient == null){
					String urlStr = MessageFormat.format("mongodb://{0}:{1}@{2}/{3}", username, password, address, authdbname);
					MongoClientURI uri = new MongoClientURI(urlStr);
					mongoClient = new MongoClient(uri);
				}
			}
		}
		return mongoClient;
	}
	
	// 
	private MongoDbFactory mongoDbFactory;
	
	/**
	 * <pre>
	 * 功能：获取MongoDbFactory对象
	 * </pre>
	 * @return
	 */
	public MongoDbFactory getMongoDbFactory(){
		if(mongoDbFactory == null){
			synchronized (MyMongoTemplate.class) {
				if(mongoDbFactory == null){
					mongoDbFactory = new SimpleMongoDbFactory(getMongoClient(), dbname);
				}
			}
		}
		return mongoDbFactory;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getDbname() {
		return dbname;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}

	public String getAuthdbname() {
		return authdbname;
	}

	public void setAuthdbname(String authdbname) {
		this.authdbname = authdbname;
	}
}
